﻿using PipeDetection.Repository;
using PipeDetection.Model;
using PipeDetection.UI.管段;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PipeDetection.UI.接口;

namespace PipeDetection.UI
{
    public partial class FrmMain : UIForm
    {
        
        public FrmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           
            if (Globel.GetDlgResult(this,new FrmNewPipeNet(), out object result))
            {
                //open project
                OpenPrj((PipeNetArea)result);
            }
        }

        private void 打开管网分段ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Globel.GetDlgResult(this, new FrmOpenPipeNet(), out object result))
            {
                //open project
                OpenPrj((PipeNetArea)result);
            }
        }
        private void OpenPrj(PipeNetArea pipeNet)
        {
            Globel.pipeNetArea=pipeNet;
            trvProject.Nodes.Clear();
            propertyGrid1.SelectedObject = null;
            var nd=trvProject.Nodes.Add(pipeNet.PipeNetAreaName);
            nd.Tag=pipeNet;
            var ps= new PipeSectionRepository().Query(x=>x.PipeNetAreaID==pipeNet.Id);
            foreach ( var p in ps )
            {
                var nnd=nd.Nodes.Add(p.PipeSectionName);
                nnd.Tag = p;
                var qs=new PipeSectionInspectionRepository().Query(x=>x.PipeSectionId==p.Id);
                foreach (var q in qs)
                {
                    var nnnd=nnd.Nodes.Add(q.RovPosition.ToString());
                    nnnd.Tag = q;
                }
            }
            trvProject.ExpandAll();
        }

        private void trvProject_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            propertyGrid1.SelectedObject = e.Node.Tag;
            propertyGrid1.Tag = e.Node.Level;
            switch (e.Node.Level)
                {
                    case 0:
                    if (e.Button == MouseButtons.Right) trvProject.ShowContextMenuStrip(uiContextMenuStrip2, e.Location);
                    
                    break;
                    case 1:
                    Globel.pipeSection = (PipeSection)e.Node.Tag;
                        if (e.Button == MouseButtons.Right) trvProject.ShowContextMenuStrip(uiContextMenuStrip3, e.Location);
                        break;
                    case 2:
                    Globel.pipeSection = (PipeSection)e.Node.Parent.Tag;
                    Globel.pipeSectionInspection = (PipeSectionInspection)e.Node.Tag;
                    if(e.Button==MouseButtons.Right) trvProject.ShowContextMenuStrip(uiContextMenuStrip1, e.Location);
                    break;
                    default:
                        break;
                }
   
       
        }

        private void 新建管段ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var s = "";
            if (UIInputDialog.ShowInputStringDialog(this, ref s, true, "请输入管段名称：", true))
            { 
                var ps=new PipeSection() { PipeNetAreaID=Globel.pipeNetArea.Id,PipeSectionName=s};
                new PipeSectionRepository().Add(ps);
                OpenPrj(Globel.pipeNetArea);
            }
        }

        private void 新建检查区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var si=0;
            var ls = Enum.GetNames(typeof(Rpositon));
            if (UISelectDialog.ShowSelectDialog(this, ref si,ls , true))
            {
                var ps = new PipeSectionInspection() { PipeSectionId = Globel.pipeSection.Id, RovPosition = (Rpositon)Enum.Parse(typeof(Rpositon), ls[si]) };
                new PipeSectionInspectionRepository().Add(ps);
                OpenPrj(Globel.pipeNetArea);
            }
        }

        private void propertyGrid1_DragDrop(object sender, DragEventArgs e)
        {
            
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            UpdateDataB();
        }
        private void UpdateDataB()
        {
            switch ((int)propertyGrid1.Tag)
            {
                case 0:
                    new PipeNetAreaRepository().Update((PipeNetArea)propertyGrid1.SelectedObject);
                    break;
                case 1:
                    new PipeSectionRepository().Update((PipeSection)propertyGrid1.SelectedObject);
                    break;
                case 2:
                    new PipeSectionInspectionRepository().Update((PipeSectionInspection)propertyGrid1.SelectedObject);
                    break;
                default:
                    break;
            }
        }
        private void trvProject_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Node.Level==2)
            {
                var pin=(PipeSectionInspection)e.Node.Tag;
                      SwitchPage(1001, typeof(FrmVideoPlay), pin);
                Text = $"{TagString}-[{Globel.pipeNetArea.PipeNetAreaName}>{Globel.pipeSection.PipeSectionName}>{Globel.pipeSectionInspection.RovPosition}]";
            }
        }
        public void SwitchPage(int pid, Type uipage,object para)
        {
            if (!ExistPage(pid))
            {
                var page = (UIPage)Activator.CreateInstance(uipage);
                AddPage(page);
                var ipag = ((Ipage)page);
                ipag.PropertyChanged += Ipag_PropertyChanged;
            }

            var pag = (Ipage)GetPage(pid);
            pag.InitD(para);
            SelectPage(pid);
        }

        private void Ipag_PropertyChanged(object sender, EventArgs e)
        {
           propertyGrid1.Refresh();
            UpdateDataB();
        }

        private void 缺陷分类维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SwitchPage(1100, typeof(FrmDetectionManager), null);
        }

        private void 管段缺陷台账表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SwitchPage(1004, typeof(FrmPipeSectionDetectTz), null);
        }
    }
}
